import { defineStore } from "pinia";

export const useHomeStore = defineStore("home", {
  state: () => {
    return {
      isMask: false, // 遮罩层是否激活
      isSearchMode: false, // 是否为搜索模式
      isAppMode: false, // 是否为捷径模式
      currentSelectedSearchResultIndex: -1, // 当前选择的搜索结果
      showAddUrlWindow: false, // 显示添加网址窗口
      showChangeUrlWindow: false, // 显示修改网址窗口
      currentRightClickItem: null, // 记录当前右键选择的网址捷径
      isLoaded: false, // 记录加载页面时，第一次请求接口

      isShowWeiboHotSearch: false,
      isShowChat: false,
      isHistorySearch: false, //历史搜索记录弹窗，默认是false(关闭状态)
      isTranslate: false, //在搜索结果列表是否显示翻译跳转,默认是不显示（false）

      isShow: false,
      searchResults: null,
      isSearch: true,
      isApp: false,
      personal: false, // 个人中心页
      facility: true, //判断移动端还是pc端设备类型
      TypeTimer: true, //显示钟表或者文字时间，默认是显示钟表true
      dialogTableVisible: false, //设置弹出框
      isStatic: true, //判断显示静态壁纸还是动态壁纸的状态，默认是true静态壁纸

      // 页面加载时，从localStorage获取token
      token: localStorage.getItem("token") ?? "",

      iconImg: "",
      userinfo: null,

      shareSentence: "全部",
      showWeather: true,
      timeType: "钟表",
      darkMode: false,
      smoothMode: false,
      searchResultsOption: "历史记录",

      // 搜索历史记录数据
      Historys: [],
      // 音乐播放列表
      index: 0, //播放歌曲索引
      isMusicSearchList: false,
      isMusicPlayList: false,
      musicList: [],
      isShowMusic:true,
      bf_List:{
        id: 28875158,
        name: "光辉岁月",
        al: {
          picUrl:
            "https://img1.baidu.com/it/u=1030351456,3187138141&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=714",
        },
        author: ["Beyond"],
      }, //准备播放列表

      Traceless: false,

      shortcuts: [
        {
          url: "https://www.bilibili.com/",
          title: "哔哩哔哩",
          ico: "https://www.bilibili.com/favicon.ico",
        },
        {
          url: "https://www.douyin.com/",
          title: "抖音",
          ico: "https://www.douyin.com/favicon.ico",
        },
        {
          url: "https://www.toutiao.com/",
          title: "今日头条",
          ico: "https://www.toutiao.com/favicon.ico",
        },
        {
          url: "https://music.163.com/",
          title: "网易云音乐",
          ico: "https://s1.music.126.net/style/favicon.ico?v20180823",
        },
        {
          url: "https://www.jd.com/",
          title: "京东",
          ico: "https://www.jd.com/favicon.ico",
        },
        {
          url: "https://www.taobao.com/",
          title: "淘宝",
          ico: "https://www.taobao.com/favicon.ico",
        },
        {
          url: "https://cn.vuejs.org/",
          title: "Vue.js",
          ico: "https://cn.vuejs.org/logo.svg",
        },
        {
          url: "https://juejin.cn/",
          title: "稀土掘金",
          ico: "https://juejin.cn/favicon.ico",
        },
        {
          url: "https://www.xuexi.cn/",
          title: "学习强国",
          ico: "https://www.xuexi.cn/favicon.ico",
        },
      ],
      shortcuts1: [
        {
          url: "/extension/video",
          title: "录制视屏",
          ico: "#icon-icon_lushipin",
        },
        {
          url: "/extension/commemorate",
          title: "行程卡纪念版",
          ico: "#icon-youjiantou",
        },
        {
          url: "/extension/jsonswitch",
          title: "json格式转换",
          ico: "#icon-json",
        },
        // {
        //   url: "/extension/file-camouflage-picture",
        //   title: "文件伪装图片",
        //   ico: "#icon-json",
        // },
        { url: "/extension/ipswitch", title: "IP地址转进制", ico: "#icon-IP" },
        { url: "/extension/nodup", title: "文本去重", ico: "#icon-wenben" },
        {
          url: "/extension/ipcount",
          title: "IP地址计算",
          ico: "#icon-jisuanqi",
        },
        {
          url: "/extension/qrcode",
          title: "二维码生成或解析",
          ico: "#icon-erweima",
        },
        {
          url: "/extension/idcard",
          title: "身份证号解析",
          ico: "#icon-shenfenzheng",
        },
        {
          url: "/extension/sayswitch",
          title: "文本转语音",
          ico: "#icon-shenfenzheng",
        },
        {
          url: "/extension/ImageOrc",
          title: "图片提取文字",
          ico: "#icon-shenfenzheng",
        },
      ],

      currentWallpaperUrl: "", // 当前壁纸链接：custom自定义、static静态、dynamic动态
      customWallpapers: [], // 自定义壁纸
      staticWallpapers: [
        {
          url: "static0.webp",
          source: "来源于网络",
        },
        {
          url: "static1.webp",
          source: "来源于网络",
        },
        {
          url: "static2.webp",
          source: "来源于网络",
        },
        {
          url: "static3.webp",
          source: "来源于网络",
        },
        {
          url: "static4.webp",
          source: "来源于网络",
        },
        {
          url: "static5.webp",
          source: "来源于网络",
        },
      ], // 静态壁纸
      dynamicWallpapers: [
        {
          url: "https://img-baofun.zhhainiao.com/pcwallpaper_ugc/preview/3760b2031ff41ca0bd80bc7a8a13f7bb_preview.mp4",
          source: "元气壁纸 | 4k云上夕阳 - 楚梦缘",
          link: "https://bizhi.cheetahfun.com/dn/pd8527.html",
        },
        {
          url: "https://img-baofun.zhhainiao.com/fs/scene/preview_video/3d877817572bb45e39bc8591a1c0a815_preview.mp4",
          source: "元气壁纸 | 日落 - 溪蓝超级甜",
          link: "https://bizhi.cheetahfun.com/dn/pd6204.html",
        },
        {
          url: "https://img-baofun.zhhainiao.com/pcwallpaper_ugc/preview/4f626c34219fdc3cbce9be2fab50debb_preview.mp4",
          source: "元气壁纸 | 4K波光粼粼 - 与日俱增",
          link: "https://bizhi.cheetahfun.com/dn/pd297311.html",
        },
        {
          url: "https://img-baofun.zhhainiao.com/pcwallpaper_ugc/preview/d0415bc9dff4a093f9527eb0c6f5336f_preview.mp4",
          source: "元气壁纸 | 云知道 你不知道 - 白桃儿",
          link: "https://bizhi.cheetahfun.com/dn/pd279164.html",
        },
        {
          url: "https://img-baofun.zhhainiao.com/fs/scene/preview_video/97ba6b60662ab4f31ef06cdf5a5f8e94_preview.mp4",
          source: "元气壁纸 | 阳光森林 - 浚博小公主",
          link: "https://bizhi.cheetahfun.com/dn/pd5405.html",
        },
        {
          url: "https://img-baofun.zhhainiao.com/pcwallpaper_ugc/preview/b99b51e6c7945c9d5565861d397451cb_preview.mp4",
          source: "元气壁纸 | 公路樱花 - 小眯眼",
          link: "https://bizhi.cheetahfun.com/dn/pd23233.html",
        },
      ], // 动态壁纸
    };
  },
  getters: {
    /**
     * 获取用户信息后，若头像存在，则使用用户当前头像
     * 若头像不存在，则使用默认头像
     * @param state
     * @returns {string}
     */
    avatar(state) {
      if (state.userinfo && state.userinfo.avatar) {
        return "https://pandax.online/uploads/avatars/" + state.userinfo.avatar;
      } else {
        return "/images/avatar.png";
      }
    },
    /**
     * 获取当前选中壁纸对象
     * @param state
     * @returns {{source: string, url: string}|{source: string, url: string}|{source: string, url: string}|{source: string, url: string}|{source: string, url: string}|*|null}
     */
    currentWallpaperObject(state) {
      if (!state.token) {
        return { main: state.staticWallpapers[0], type: "static" };
      }

      const customWallpaper = getStringAfterStartsWith(
        state.currentWallpaperUrl,
        "custom://"
      );
      const customWallpaperIndex = parseInt(customWallpaper);
      if (
        customWallpaper &&
        state.customWallpapers.length >= customWallpaperIndex
      ) {
        return {
          main: state.customWallpapers[customWallpaperIndex],
          type: "custom",
        };
      }

      const staticWallpaper = getStringAfterStartsWith(
        state.currentWallpaperUrl,
        "static://"
      );
      if (staticWallpaper) {
        return {
          main: state.staticWallpapers[parseInt(staticWallpaper)],
          type: "static",
        };
      }

      const dynamicWallpaper = getStringAfterStartsWith(
        state.currentWallpaperUrl,
        "dynamic://"
      );
      if (dynamicWallpaper) {
        return {
          main: state.dynamicWallpapers[parseInt(dynamicWallpaper)],
          type: "dynamic",
        };
      }

      return null;
    },
  },
  actions: {
    /**
     * 更新token
     * @param token
     */
    updateToken(token) {
      this.token = token;
    },
    /**
     * 添加网址捷径
     * @param shortcut
     */
    addShortcut(shortcut) {
      this.shortcuts.push(shortcut);
    },
    /**
     * 根据id删除网址捷径
     * @param id
     */
    deleteShortcut(id) {
      this.shortcuts = this.shortcuts.filter((item) => item.id !== id);
    },
    /**
     * 添加自定义壁纸
     * @param wallpaper
     */
    addCustomWallpaper(wallpaper) {
      this.customWallpapers.push(wallpaper);
    },
    /**
     * 更改是否显示分享句子
     * @param newValue
     */
    setShareSentence(newValue) {
      this.shareSentence = newValue;
    },
    /**
     * 更改是否显示今日天气
     * @param newValue
     */
    setShowWeather(newValue) {
      this.showWeather = newValue;
    },
    /**
     * 设置时钟类型
     * @param newValue
     */
    setTimeType(newValue) {
      this.timeType = newValue;
    },
    /**
     * 设置搜索结果附加选项
     * @param newValue
     */
    setSearchResultsOption(newValue) {
      this.searchResultsOption = newValue;
    },
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: "pandaHistory",
        storage: localStorage,
        paths: ["Historys"],
      },
      {
        key: "traceless",
        storage: localStorage,
        paths: ["Traceless"],
      },
      {
        key: "isShowMusic",
        storage: localStorage,
        paths: ["isShowMusic"],
      },
      {
        key: "musicList",
        storage: localStorage,
        paths: ["musicList"],
      },
    ],
  },
});

/**
 * 判断字符串是否以xxx开头，如果是，则返回xxx后面的字符串，如果不是，则返回false
 * @param s 要判断的字符串
 * @param starts 以什么为开头
 * @returns {*|boolean}
 */
const getStringAfterStartsWith = (s, starts) => {
  if (s.startsWith(starts)) {
    return s.slice(starts.length);
  }
  return false;
};
